package mysql

import (
	"gorm.io/gorm"
	"taobao-merchandise/taobaorpc/taobaorpc"
)

type Goods struct {
	gorm.Model
	Title  string  `gorm:"type:varchar(50)"`
	Price  float32 `gorm:"type:decimal(10,2)"`
	Image  string  `gorm:"type:varchar(255)"`
	UserId int64   `gorm:"type:int(11)"`
}

func (g *Goods) GoodsAdd(db *gorm.DB) error {
	return db.Debug().Create(g).Error
}

func (g *Goods) FindGoods(db *gorm.DB, id int64) error {
	return db.Debug().Where("id=?", id).First(g).Error
}

func (g *Goods) GoodsUpdate(db *gorm.DB) error {
	return db.Debug().Updates(g).Error
}

func (g *Goods) GoodsDel(db *gorm.DB, id int64) error {
	return db.Debug().Where("id=?", id).Delete(g).Error
}
func GoodsList(db *gorm.DB) ([]*taobaorpc.GoodsList, error) {
	var list []*taobaorpc.GoodsList
	err := db.Debug().Model(&Goods{}).
		Select("id as goods_id", "title", "price", "image").
		Scopes(Paginate(1, 5)).Find(&list).Error
	return list, err
}
